home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex23.c < prev    next >
C/C++ Source or Header  |  1995-04-24  |  3KB  |  73 lines

  1. #include <genstub.c>
  2.  
  3. // Child thread procedure. The child waits to get event. It then sits idle
  4. // for five seconds and sets the event again so another thread can use it.
  5. DWORD WINAPI ChildThreadProc( HWND hWnd )
  6. {
  7.    char szBuffer[256];         // work area for print formatting
  8.    HANDLE hAutoEvent = OpenEvent( SYNCHRONIZE, FALSE, "EXAMPLE-AUTOEVENT");
  9.    wsprintf( szBuffer, "Thread %x waiting for Event %x",
  10.             GetCurrentThreadId(  ), hAutoEvent );
  11.    SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
  12.    // Check that write auto reset is signaled.
  13.    WaitForSingleObject( hAutoEvent, INFINITE );
  14.    wsprintf( szBuffer,"Thread %x got event", GetCurrentThreadId( ) );
  15.    SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
  16.    Sleep( 5000 );
  17.    // Release event.
  18.    wsprintf( szBuffer,"Thread %x is done with event", GetCurrentThreadId( ) );
  19.    SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
  20.    SetEvent( hAutoEvent );
  21.    CloseHandle( hAutoEvent );
  22.    ExitThread( TRUE );
  23. }
  24.  
  25. // Windows message procedure.
  26. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  27. {
  28.    static HANDLE hAutoEvent = 0;
  29.  
  30.    switch (uMsg)
  31.    {
  32.            case WM_CREATE: // Make an auto-reset event with initial state of signaled.
  33.                    hAutoEvent = CreateEvent( NULL, FALSE, TRUE, "EXAMPLE-AUTOEVENT");
  34.                    return DefWindowProc( hWnd, uMsg, wParam, lParam );
  35.            case WM_DESTROY:
  36.                    if ( hAutoEvent )
  37.                       CloseHandle( hAutoEvent );
  38.                    PostQuitMessage( 0 );
  39.                    break;
  40.            case WM_USER:
  41.                    {  // Message to show synchronization actions.
  42.                       TCHAR szBuffer[101];
  43.                       static int row = 0;
  44.                       static int msg_num = 1;
  45.                       HDC hDC = GetDC( hWnd );
  46.  
  47.                       FillMemory( szBuffer, 100, 32 );
  48.                       TextOut( hDC, 0, row, szBuffer, 100 );
  49.                       wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
  50.                       TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
  51.                       row = ( row > 200 ) ? 0 : row+=20;
  52.                       ReleaseDC( hWnd, hDC );
  53.                    }
  54.                    break;
  55.            case WM_COMMAND:       // process menu items
  56.                    switch ( LOWORD( wParam )  )
  57.                    {
  58.                       case IDM_TEST:
  59.                       {
  60.                            DWORD id = 0;
  61.                            CreateThread( NULL, 0, ChildThreadProc, hWnd, 0, &id );
  62.                       }
  63.                       break;
  64.                       case IDM_EXIT:
  65.                            DestroyWindow( hWnd );
  66.                       break;
  67.                    }
  68.            break;
  69.            default:
  70.                 return DefWindowProc( hWnd, uMsg, wParam, lParam );
  71.    }
  72.    return NULL;
  73. }